package com.ww.flink

import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
import org.apache.flink.streaming.api.scala._


object Flink_try01 {
  def main(args: Array[String]): Unit = {
    /**
     * createLocalEnvironment 创建一个本地执行的环境  local
     * createLocalEnvironmentWithWebUI 创建一个本地执行的环境  同时还开启Web UI的查看端口  8081
     * getExecutionEnvironment 根据你执行的环境创建上下文，比如local  cluster
     */
    val env = StreamExecutionEnvironment.getExecutionEnvironment

    val socket: DataStream[String] = env.socketTextStream("node01", 8888)
    val word: DataStream[String] = socket.flatMap(_.split(" ")).setParallelism(2)
    val maped: DataStream[(String, Int)] = word.map((_, 1))
    val res: DataStream[(String, Int)] = maped.keyBy(0).sum(1)
    res.print()
    //启动Flink 任务
    env.execute("first flink job")
  }
}
